package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTapp;
import com.iot.service.TappService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/app/")
public class TAppController extends BaseController {
    @Autowired
    private TappService service;

    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {
            //  String menu = request.getParameter("menu");

            MTapp entity = JsonHelper.toBean(bean, MTapp.class);

            if (entity.getAPP_ID() > 0) {
                service.updateData(entity);
            } else {
                service.insertData(entity);
            }
            return LResult.Success(entity.getAPP_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTapp bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            service.deleteById(id);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("query")
    public String queryByPage() {

        try {
            //" AND SNAME LIKE '" + key + "%'"
            //   Map map = request.getParameterMap(); // 获取所有参数
            PageData map = getPageData();

            if (!map.containsKey("sname")) {
                map.put("sname", "");
            }
            if (!map.containsKey("provinceId")) {
                map.put("provinceId", 0);
            }
            if (!map.containsKey("cityId")) {
                map.put("cityId", 0);
            }
            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return LResult.Error("数据错误").toJson();
        }
    }


    @ResponseBody
    @RequestMapping("initList")
    public String initList() {
        try {
            Map<String, Object> map = service.initList();
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initEdit")
    public String initEdit() {
        try {
            Map<String, Object> map = service.initEdit();
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
}
